﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace NutriStat.Statistics
{
    public class CDC2000GrowthReference : GrowthReferenceBase
    {
        public static int FlagBMIUpper = 5;
        public static int FlagBMILower = -4;
        public static int FlagWeightHeightUpper = 5;
        public static int FlagWeightHeightLower = -4;
        public static int FlagWeightAgeUpper = 5;
        public static int FlagWeightAgeLower = -5;
        public static int FlagLengthAgeUpper = 3;
        public static int FlagLengthAgeLower = -5;
        public static int FlagHeightAgeUpper = 3;
        public static int FlagHeightAgeLower = -5;
        public static int FlagSSFUpper;
        public static int FlagSSFLower;
        public static int FlagTSFUpper;
        public static int FlagTSFLower;
        public static int FlagHeadUpper;
        public static int FlagHeadLower;
        public static int FlagMUACUpper;
        public static int FlagMUACLower;

        public CDC2000GrowthReference() 
            : base()
        {
            Name = "CDC";
            FullName = "CDC 2000";

            // CDC Numbers
            HeightAgeLower = 24;
            HeightAgeUpper = 239;
            LengthAgeLower = 0;
            LengthAgeUpper = 36;

            WeightHeightLower = 77;
            WeightHeightUpper = 121;
            WeightLengthLower = 45;
            WeightLengthUpper = 103;

            WeightAgeLower = 0;
            WeightAgeUpper = 239;

            HeadLower = 0;
            HeadUpper = 36;

            BMILower = 24;
            BMIUpper = 239;

            // Invalid - CDC 2000 doesn't use these
            SSFLower = -1;
            SSFUpper = -1;

            TSFLower = -1;
            TSFUpper = -1;

            MUACLower = -1;
            MUACUpper = -1;

            FlagBMIUpper = 5;
            FlagBMILower = -4;
            FlagWeightHeightUpper = 5;
            FlagWeightHeightLower = -4;
            FlagWeightAgeUpper = 5;
            FlagWeightAgeLower = -5;
            FlagLengthAgeUpper = 0;
            FlagLengthAgeLower = 0;
            FlagHeightAgeUpper = 3;
            FlagHeightAgeLower = -5;
            FlagSSFUpper = 0;
            FlagSSFLower = 0;
            FlagTSFUpper = 0;
            FlagTSFLower = 0;
            FlagHeadUpper = 0;
            FlagHeadLower = 0;
            FlagMUACUpper = 0;
            FlagMUACLower = 0; 
        }       

        public static string CheckBMIFlagged(string sd)
        {
            string flag = NutriStringConstants.Empty;
            try
            {
                double stdDev = Convert.ToDouble(sd);
                if (sd == NutriStringConstants.Empty)
                    flag = NutriStringConstants.Empty;
                else if (stdDev >= FlagBMIUpper)
                    flag = NutriStringConstants.H;
                else if (stdDev <= FlagBMILower)
                    flag = NutriStringConstants.L;
                else
                    flag = NutriStringConstants.OK;
            }
            catch
            {
            }            
            return flag;
        }

        public static string CheckWeightHeightFlagged(string sd)
        {
            string flag = NutriStringConstants.Empty;
            try
            {
                double stdDev = Convert.ToDouble(sd);
                if (sd == NutriStringConstants.Empty)
                    flag = NutriStringConstants.Empty;
                else if (stdDev >= FlagWeightHeightUpper)
                    flag = NutriStringConstants.H;
                else if (stdDev <= FlagWeightHeightLower)
                    flag = NutriStringConstants.L; 
                else
                    flag = NutriStringConstants.OK;
            }
            catch
            {
            }
            return flag;
        }

        public static string CheckWeightAgeFlagged(string sd)
        {
            string flag = NutriStringConstants.Empty;
            try
            {
                double stdDev = Convert.ToDouble(sd);
                if (sd == NutriStringConstants.Empty)
                    flag = NutriStringConstants.Empty;
                else if (stdDev >= FlagWeightAgeUpper)
                    flag = NutriStringConstants.H;
                else if (stdDev <= FlagWeightAgeLower)
                    flag = NutriStringConstants.L;                
                else
                    flag = NutriStringConstants.OK;
            }
            catch
            {
            }
            return flag;
        }

        public static string CheckHeightAgeFlagged(string sd)
        {
            string flag = NutriStringConstants.Empty;
            try
            {
                double stdDev = Convert.ToDouble(sd);
                if (sd == NutriStringConstants.Empty)
                    flag = NutriStringConstants.Empty;
                else if (stdDev >= FlagHeightAgeUpper)
                    flag = NutriStringConstants.H;
                else if (stdDev <= FlagHeightAgeLower)
                    flag = NutriStringConstants.L;
                else
                    flag = NutriStringConstants.OK;
            }
            catch
            {
            }
            return flag;
        }

        public override string GetPercentOfMedianForWeightHeight(double rawScore) { return NutriStringConstants.Undefined; }
        public override string GetPercentOfMedianForWeightAge(double rawScore) { return NutriStringConstants.Undefined; }
        public override string GetPercentOfMedianForHeightAge(double rawScore) { return NutriStringConstants.Undefined; }

        public override string GetZForBMI(ref double z, ref double p, ref double flag, double rawScore, double age, short gender)
        {
            try
            {
                if (age >= BMILower && age <= BMIUpper)
                {
                    z = AnthStat.NutriDataCalc.GetCDCBMIZ(rawScore, age, gender, ref flag);
                    p = AnthStat.NutriDataCalc.GetPercentile(z);
                    return z.ToString();
                }
                else
                    return NutriStringConstants.Undefined;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            return NutriStringConstants.Undefined;
        } // end GetZ

        public override string GetZForHead(ref double z, ref double p, ref double flag, double rawScore, double age, short gender)
        {
            try
            {
                if (age >= HeadLower && age <= HeadUpper)
                {
                    z = AnthStat.NutriDataCalc.GetCDCHeadZ(rawScore, age, gender, ref flag);
                    p = AnthStat.NutriDataCalc.GetPercentile(z);
                    return z.ToString();
                }
                else
                    return NutriStringConstants.Undefined;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            return NutriStringConstants.Undefined;
        } // end GetZ

        public override string GetZForHeightAge(ref double z, ref double p, ref double flag, double rawScore, double age, short gender, bool recumbent)
        {
            if (recumbent)
            {
                try
                {
                    if (age >= LengthAgeLower && age <= LengthAgeUpper)
                    {
                        z = AnthStat.NutriDataCalc.GetCDCLgthAgeZ(rawScore, age, gender, ref flag);
                        p = AnthStat.NutriDataCalc.GetPercentile(z);
                        return z.ToString();
                    }
                    else
                        return NutriStringConstants.Undefined;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
                return NutriStringConstants.Undefined;
            }
            else
            {
                try
                {
                    if (age >= HeightAgeLower && age <= HeightAgeUpper)
                    {
                        z = AnthStat.NutriDataCalc.GetCDCHtAgeZ(rawScore, age, gender, ref flag);
                        p = AnthStat.NutriDataCalc.GetPercentile(z);
                        return z.ToString();
                    }
                    else
                        return NutriStringConstants.Undefined;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
                return NutriStringConstants.Undefined;
            }
        } // end GetZ

        public override string GetZForWeightAge(ref double z, ref double p, ref double flag, double rawScore, double age, short gender)
        {
            try
            {
                if (age >= WeightAgeLower && age <= WeightAgeUpper)
                {
                    z = AnthStat.NutriDataCalc.GetCDCWtAgeZ(rawScore, age, gender, ref flag);
                    p = AnthStat.NutriDataCalc.GetPercentile(z);
                    return z.ToString();
                }
                else
                    return NutriStringConstants.Undefined;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            return NutriStringConstants.Undefined;
        } // end GetZ
        
        public override string GetZForWeightHeight(ref double z, ref double p, ref double flag, double rawScore, double age, double height, short gender, bool recumbent)
        {
            if (recumbent)
            {
                try
                {
                    if (height >= WeightLengthLower && height <= WeightLengthUpper
                        &&
                        age >= WeightLengthAgeLower && age <= WeightLengthAgeUpper)
                    {
                        z = AnthStat.NutriDataCalc.GetCDCWtLgthZ(rawScore, height, gender, ref flag);
                        p = AnthStat.NutriDataCalc.GetPercentile(z);
                        return z.ToString();
                    }
                    else
                        return NutriStringConstants.Undefined;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
                return NutriStringConstants.Undefined;
            }
            else
            {
                try
                {
                    if (height >= WeightHeightLower && height <= WeightHeightUpper
                        &&
                        age >= WeightHeightAgeLower && age <= WeightHeightAgeUpper)
                    {
                        z = AnthStat.NutriDataCalc.GetCDCWtHtZ(rawScore, height, gender, ref flag);
                        p = AnthStat.NutriDataCalc.GetPercentile(z);
                        return z.ToString();
                    }
                    else
                        return NutriStringConstants.Undefined;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
                return NutriStringConstants.Undefined;
            }
        } // end GetZ
        
    }
}
